package com.onefinger.manager.views.model
{
	import com.onefinger.manager.events.ManagerEvent;
	import com.onefinger.manager.vos.Waiter;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	public class WaiterCRUDModel extends BaseCRUDModel
	{
		//--------------------------------------------------------------------
		//    Public Setters and Getters
		//--------------------------------------------------------------------
		
		
		//.................waiters......................
		private var _waiters:ArrayCollection;
		
		[Bindable(event="changeWaiters")]	
		public function get waiters():ArrayCollection
		{
			return _waiters;
		}
		public function set waiters(value:ArrayCollection):void
		{
			_waiters = value;
			dispatchEvent( new Event( "changeWaiters" ) );
		}
		
		//.................selectedWaiter......................
		private var _selectedWaiter:Waiter;
		
		[Bindable(event="changeWaiter")]
		public function get selectedWaiter():Waiter
		{
			return _selectedWaiter;
		}
		
		public function set selectedWaiter(value:Waiter):void
		{
			_selectedWaiter = value;
			dispatchEvent( new Event( "changeWaiter" ) );
		}
		
		//.................tempWaiter......................
		private var _tempWaiter:Waiter=new Waiter();
		[Bindable(event="changeTempWaiter")]		
		public function get tempWaiter():Waiter
		{
			return _tempWaiter;
		}
		
		public function set tempWaiter(value:Waiter):void
		{
			_tempWaiter = value;
			dispatchEvent(new Event("changeTempWaiter"));
		}
		
		//--------------------------------------------------------------------
		//    Constructor
		//--------------------------------------------------------------------
		public function WaiterCRUDModel(dispatcher:IEventDispatcher)
		{
			super(dispatcher);
		}
		//--------------------------------------------------------------------
		//    Public Methods
		//--------------------------------------------------------------------
		
		//.................selectWaiter......................
		override public function select(waiter:Object):void
		{
			super.select(waiter);
			tempWaiter= waiter.clone();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_WAITER );
			event.waiter = waiter as Waiter;
			dispatcher.dispatchEvent( event );
		}
		override public function edit(waiter:Object):void
		{
			super.edit(waiter);
			select(waiter);
			stateIndex = BaseCRUDModel.EDITING;
		}
		//.................deleteWaiter......................
		override public function remove():void
		{
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.DELETE_WAITER );
			event.waiter = selectedWaiter;
			dispatcher.dispatchEvent( event );
			unselect();
		}
		
		//.................addNewWaiter......................
		override public function addNew():void
		{
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.SAVE_WAITER);
			evet.waiter = tempWaiter;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateWaiter......................
		override public function update():void
		{
			selectedWaiter.name=tempWaiter.name;
			selectedWaiter.photo=tempWaiter.photo;
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.UPDATE_WAITER);
			evet.waiter = selectedWaiter;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateWaiter......................
		override public function unselect():void
		{
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_WAITER );
			event.waiter = new Waiter();
			dispatcher.dispatchEvent( event );
			tempWaiter = new Waiter();
		}
		//.................cancel......................
		override public function cancel():void
		{
			selectedWaiter=new Waiter();
			tempWaiter = new Waiter();
			stateIndex=BaseCRUDModel.CREATING;
			
		}
		
		//--------------------------------------------------------------------
		//   update on change
		//--------------------------------------------------------------------
		
		public function updateSelectedName(value:String):void
		{
			tempWaiter.name=value;
		}
		public function updateSelectedPhoto(value:String):void
		{
			tempWaiter.photo=value;	
		}
		
	}
}